﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Coldairarrow.DotNettyRPC;
using Common;
using ZooKeeperManage;
using ZooKeeperManage.Dispersed;
using ZooKeeperManage.Helper;

namespace Samples
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DispersedServer dispersedServer = new DispersedServer(9999);
                dispersedServer.RegisterService<IHello, Hello>();
                dispersedServer.RegisterService<IHello2, Hello2>();
                dispersedServer.Start();
            }
            catch (Exception ex)
            {
                var s = ex.Message;
            }
        }

        private ServerData _server;
        private void button2_Click(object sender, EventArgs e)
        {
            var work = new WorkData()
            {
                Name = typeof(IHello).Name,
                Data = new ServerData()
                {
                    //Ip = Network.GetIPv4AddressList(),
                    //Port = _port
                }.ToJson()
            };

            WorkClient workClient = new WorkClient(work);
            workClient.ServerDataEven += WorkClient_ServerDataEven;
            work = workClient.GetWork();
            _server = work.Data.ToObject<ServerData>();

        }

        private void WorkClient_ServerDataEven(WorkData obj)
        {
            _server = obj.Data.ToObject<ServerData>();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (_server.Ip != null)
            {
                IHello client = RPCClientFactory.GetClient<IHello>(_server.Ip[0], _server.Port);
                client.SayHello("Hello");

                IHello2 client2 = RPCClientFactory.GetClient<IHello2>(_server.Ip[0], _server.Port);
                client2.SayHello("Hello2");
                MessageBox.Show(@"完成");
            }
            else
            {
                MessageBox.Show(@"没有服务");
            }
        }
    }
}
